package com.asset.admin.system.syemenu.biz.impl;

import com.asset.admin.dao.entity.SysMenuEntity;
import com.asset.admin.dao.mapper.ISysMenuMapper;
import com.asset.admin.system.syemenu.biz.ISysMenuBiz;
import com.asset.admin.system.syemenu.enums.EnumEnableStatus;
import com.asset.admin.system.syemenu.form.MenuForm;
import com.asset.admin.system.syemenu.form.QueryMenuForm;
import com.asset.common.helper.CurrentUserHelper;
import com.asset.common.utils.DateUtil;
import com.asset.common.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * todo 描述
 * </p>
 *
 * @author 牧之
 * @date 2024/2/27 14:26
 */
@Service
public class SysMenuBizImpl implements ISysMenuBiz {

    @Resource
    private ISysMenuMapper menuMapper;

    @Override
    public List<SysMenuEntity> getList(QueryMenuForm queryMenuForm) {
        return menuMapper.getList(queryMenuForm);
    }

    @Override
    public int getCount(QueryMenuForm queryMenuForm) {
        return menuMapper.getCount(queryMenuForm);
    }

    @Override
    public void createMenu(MenuForm menuForm) {

        Long currentUserId = CurrentUserHelper.getCurrentUserId();
        if (StringUtil.isBlank(menuForm.getName())) {
            throw new RuntimeException("菜单名称不能为空");
        }

        SysMenuEntity menuEntity = new SysMenuEntity();
        menuEntity.setName(menuForm.getName());
        menuEntity.setPid(menuForm.getPid() == null? 0 : menuForm.getPid());
        menuEntity.setUrl(menuForm.getUrl());
        menuEntity.setMenuType(0);
        menuEntity.setIcon(menuForm.getIcon());
        menuEntity.setSort(menuForm.getSort());
        menuEntity.setStatus(EnumEnableStatus.getCodeByValue(menuForm.getStatus()));
        menuEntity.setCreator(currentUserId);
        menuEntity.setCreateDate(DateUtil.getCurrentTime());
        menuEntity.setUpdateDate(DateUtil.getCurrentTime());
        menuEntity.setPermissions(menuForm.getPermissions());
        menuMapper.createMenu(menuEntity);
    }

    @Override
    public void updateMenu(MenuForm menuForm) {
        SysMenuEntity menuEntity = new SysMenuEntity();
        menuEntity.setId(menuForm.getId());
        menuEntity.setName(menuForm.getName());
        menuEntity.setPid(menuForm.getPid() == null? 0 : menuForm.getPid());
        menuEntity.setUrl(menuForm.getUrl());
        menuEntity.setMenuType(0);
        menuEntity.setIcon(menuForm.getIcon());
        menuEntity.setSort(menuForm.getSort());
        menuEntity.setStatus(EnumEnableStatus.getCodeByValue(menuForm.getStatus()));
//        menuEntity.setCreator(currentUserId);
        menuEntity.setUpdateDate(DateUtil.getCurrentTime());
        menuEntity.setPermissions(menuForm.getPermissions());
        menuMapper.updateMenu(menuEntity);
    }

    @Override
    public void deleteMenu(Long menuId) {
        menuMapper.deleteMenu(menuId);
    }

}
